package edu.uoc.tdp.pac3.server;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.Statement;
import javax.swing.JOptionPane;

import edu.uoc.tdp.pac3.beans.User;
import edu.uoc.tdp.pac3.common.tools.TDSLanguageUtils;


/**
 * Classe per gestionar l'acces a la Taula d'usuaris
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class GestorUsuaris {
    
    private Connection objConn;
    private TDSLanguageUtils mess = null;
    
     public GestorUsuaris(GestorDisc gestor) {        
        objConn = gestor.getConnection();        
    }
     
    public Connection getConnection() {
        return objConn;
    }
    /**
     * Recerca d'un usuari segons l'identificador
     * @param Identificador de l'usuari
     * @return L'bjecte de la classe User amb identificador igual que el parametre
     */
    public User getUser(int idUser) {
        User user = null;
        
        String consulta = "SELECT user_id, user_name, creation_date "
                + "FROM tb_user WHERE user_id =  " + idUser;
        try {
            Statement query = getConnection().createStatement();
            ResultSet rs = query.executeQuery(consulta);
            if (rs.next()) {
                user = new User(rs.getInt(1), rs.getString(2), rs.getDate(3));
            }
            rs.close();
            query.close();

        } catch (SQLException eSQL) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.user.err.db") + "\n" + eSQL.getMessage(),
                    mess.getMessage("gestor.user"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eE) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.user.err.unknown") + "\n" + eE.getMessage(),
                    mess.getMessage("gestor.user"), JOptionPane.ERROR_MESSAGE);
        }

        return user;
    }

    /**
     * LLista de tots els uausris de la DB
     * @return Lista en tots els uauaris
     */
    public List<User> getUsersList() {

        List<User> userList = new ArrayList<User>();        
        try {
            String consulta = "SELECT user_id, user_name, creation_date FROM tb_user order by user_name";

            Statement query = getConnection().createStatement();
            ResultSet rs = query.executeQuery(consulta);
            while (rs.next()) {
                Integer idUser = new Integer(rs.getInt("user_id"));                               
                userList.add( new User(idUser, rs.getString("user_name"), rs.getDate("creation_date")));
            }
        } catch (SQLException e1) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.user.err.db") + "\n" + e1.getMessage(),
                    mess.getMessage("gestor.user.err"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eAl) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.user.err.unknown") + "\n" + eAl.getMessage(),
                    mess.getMessage("gestor.user.err"), JOptionPane.ERROR_MESSAGE);
        }
        
        return userList;
    }
}
